package cn.mygweb.admin.modules.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.mygweb.admin.modules.web.pojo.Content;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;
import cn.mygweb.common.pojo.RedisKey;
import cn.mygweb.common.utils.JsonUtils;
import cn.mygweb.common.utils.UploadUtils;

/** 
* <p>Title: ContentController.java</p>
* <p>Description: 内容管理控制层</p>  
* @author gxb 
* @date 2019年6月26日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/web/content")
public class ContentController extends BaseController{
	
	//文件上传地址
	@Value("${file.upload.path}")
	private String uploadFilePath;
	
	/**
	 * <p>Title: contentList</p>
	 * <p>Description: 前台内容管理列表页面</p>
	 * @return
	 */
	@RequestMapping("/list")
	public String contentList() {
		return "admin/modules/web/content";
	}
	
	/**
	 * <p>Title: ajaxContentList</p>
	 * <p>Description: 异步获取内容管理列表</p>
	 * @return
	 */
	@RequestMapping("/ajaxContentList")
	@ResponseBody
	public JsonResult ajaxContentList() {
		List<Content> contentList;
		try {
			if(super.isNullOrBlank(RedisKey.ADMIN_WEB_CONTENT)) {
				contentList = contentService.queryListByExample(new Content());
				super.redisSet(RedisKey.ADMIN_WEB_CONTENT, JsonUtils.objectToJson(contentList));
			}
			contentList = JsonUtils.jsonToList(super.redisGet(RedisKey.ADMIN_WEB_CONTENT), Content.class);
		} catch (Exception e) {
			e.printStackTrace();
			contentList = contentService.queryListByExample(new Content());
		}
		return JsonResult.ok(contentList);
	}
	
	/**
	 * <p>Title: operation</p>
	 * <p>Description: 文章信息的增、删、改操作</p>
	 * @param content
	 * @param oper
	 * @return
	 */
	@RequestMapping("/operation")
	@ResponseBody
	public JsonResult operation(Content content, String oper) {
		if("add".equals(oper)) {
			try {
				content.setId(sid.nextShort());
				contentService.save(content);
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("添加成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("添加失败");
			}
		} else if("edit".equals(oper)) {
			try {
				contentService.update(content);
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("修改成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("修改失败");
			}
		} else if("del".equals(oper)) {
			try {
				String ids = content.getId();
				contentService.batchDelete(ids);
				super.delRedisCacheByKeys(RedisKey.ADMIN_WEB_CONTENT, RedisKey.KEY_WEB_INDEX);
				return JsonResult.ok("删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				return JsonResult.errorMsg("删除失败");
			}
		} else {
			return null;
		}
	}
	
	/**
	 * <p>Title: uploadImg</p>
	 * <p>Description: 上传文章图片</p>
	 * @param file
	 * @return
	 */
	@RequestMapping("/uploadImg")
	@ResponseBody
	public JsonResult uploadImg(MultipartFile file,String id) {
		if(file.isEmpty()) {
			return null;
		}
		try {
			String imgDir = UploadUtils.uploadImg(file, uploadFilePath);
			Content content = contentService.queryById(id);
			content.setImg(imgDir);
			contentService.update(content);
			super.delAllRedisCache();
			return JsonResult.ok("上传成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("上传失败");
		}
	}
	
}
